package com.itheima.controller.user;

import com.itheima.api.user.UmsResourceService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.UmsResource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @DubboReference
    private UmsResourceService umsResourceService;

    /*
    添加后台资源
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody UmsResource umsResource){
        umsResource.setCreateTime(LocalDateTime.now());
        umsResourceService.save(umsResource);
        return AjaxResult.success("操作成功");
    }

    /*
    根据ID删除后台资源
     */
@PostMapping("/delete/{id}")
    public  AjaxResult delete(@PathVariable Long id){
        umsResourceService.removeById(id);
        return AjaxResult.success("操作成功");
}

/*
查询所有后台资源
 */
    @GetMapping("/listAll")
    public AjaxResult listAll(){
        List<UmsResource> list = umsResourceService.list();
        for (UmsResource umsResource : list) {
            umsResource.setCreateTime(LocalDateTime.now());
        }
        return  AjaxResult.success(list);
    }

    /*
    修改后台资源
     */
    @PostMapping("/update/{id}")
    public AjaxResult update(@RequestBody UmsResource umsResource,@PathVariable Long id){
        umsResource.setCreateTime(LocalDateTime.now());
        umsResource.setId(id);
        umsResourceService.updateById(umsResource);
        return AjaxResult.success("操作成功");
    }

    /*
    根据ID获取资源详情
     */
    @GetMapping("/{id}")
    public  AjaxResult getById(@PathVariable Long id){
        UmsResource umsResource = umsResourceService.getById(id);
        return AjaxResult.success(umsResource);
    }

    /*
    分页模糊查询后台资源
     */
    @GetMapping("/list")
    public  AjaxResult list(Integer categoryId,String nameKeyword,Integer pageNum,Integer pageSize,String urlKeyword){
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo=umsResourceService.queryPage(categoryId,nameKeyword,pageNum,pageSize,urlKeyword);
        return AjaxResult.success(tableDataInfo);
    }
}
